package com.iplant.base.po;

import com.iplant.base.utils.BaseConfigConstans;
import com.iplant.base.utils.annotation.po.DBBasePO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.POUtils;

public class Pagination {

    public static final int DEFAULT_MAX_PAGE_ = 1000000;

    private Pagination() {
        Sort = "ID"; // 默认按Id排序
        SortType = " desc ";
        PageIndex = 1;
        PageSize = 10;
    }

    private Pagination(int wPageSize) {
        Sort = "ID"; // 默认按Id排序
        SortType = " desc ";
        PageIndex = 1;
        if (wPageSize <= 0) {
            wPageSize = 10;
        }
        PageSize = wPageSize;
    }

    private static int MaxPageSize = 100;

    static {

        MaxPageSize = BaseConfigConstans.MaxPageSize;

        if (MaxPageSize < 100) {
            MaxPageSize = 500;
        }
    }


    public static Pagination Create(int wPageIndex, int wPageSize, String... args) {
        if (wPageSize <= 0) {
            wPageSize = MaxPageSize;
        }
        if (wPageIndex <= 0) {
            wPageIndex = 1;
        }
        Pagination wResult = new Pagination();

        wResult.PageIndex = wPageIndex;
        wResult.PageSize = wPageSize;
        if (args == null) {
            return wResult;
        }
        if (args.length > 0 && StringUtils.isNotEmpty(args[0])) {
            wResult.Sort = args[0];
        }
        if (args.length > 1 && StringUtils.isNotEmpty(args[1])) {
            wResult.SortType = args[1];
        }
        if (args.length > 2 && StringUtils.isNotEmpty(args[2]))
            wResult.PageFuzzy = StringUtils.parseInt(args[2]);
        return wResult;
    }

    public static Pagination Create(int wPageIndex, int wPageSize, Class<? extends BasePO> clazz) {
        if (wPageSize <= 0) {
            wPageSize = MaxPageSize;
        }
        if (wPageIndex <= 0) {
            wPageIndex = 1;
        }
        Pagination wResult = new Pagination();

        wResult.PageIndex = wPageIndex;
        wResult.PageSize = wPageSize;

        DBBasePO dbBasePO = POUtils.GetInstance().getDBBasePO(clazz);
        if (dbBasePO != null && StringUtils.isNotEmpty(dbBasePO.OrderBy())) {
            wResult.Sort = dbBasePO.OrderBy();
        }
        return wResult;
    }

    private static Pagination mDefault = new Pagination();

    public static Pagination getDefault() {

        return mDefault;

    }

    private static Pagination mMaxSize = new Pagination(Integer.MAX_VALUE);

    public static Pagination getMaxSize() {
        return mMaxSize;
    }

    public static Pagination getMaxSize(Class<? extends BasePO> clazz) {

        return Create(1, Integer.MAX_VALUE, clazz);
    }

    public static Pagination getNewMaxSize() {
        return new Pagination(Integer.MAX_VALUE);
    }

    public static Pagination getNewDefault() {
        return new Pagination();
    }

    /**
     * 每页行数
     **/
    private int PageSize = 0;
    /**
     * 当前页
     **/
    private int PageIndex = 0;
    /**
     * 排序列
     **/
    private String Sort = "";
    /**
     * 排序类型
     **/
    private String SortType = "";
    /**
     * 总记录数
     **/
    private int TotalCount = 0;


    private int PageFuzzy = 0;

    /**
     * 总页数
     **/
    public int getTotalPage() {

        if (TotalCount > 0) {
            return TotalCount % this.PageSize == 0 ? TotalCount / this.PageSize : TotalCount / this.PageSize + 1;
        } else {
            return 0;
        }

    }

    public int getPageSize() {
        return PageSize;
    }

    public void setPageSize(int pageSize) {
        PageSize = pageSize;
    }

    public int getPageIndex() {
        return PageIndex;
    }

    public void setPageIndex(int pageIndex) {
        PageIndex = pageIndex;
    }

    public String getSort() {
        return Sort;
    }

    public void setSort(String sort) {
        Sort = sort;
    }

    public String getSortType() {
        return SortType;
    }

    public void setSortType(String sortType) {
        SortType = sortType;
    }

    public int getTotalCount() {
        return TotalCount;
    }

    public synchronized void setTotalCount(int totalCount) {
        TotalCount = totalCount;
    }

    public int getPageFuzzy() {
        return PageFuzzy;
    }

    public void setPageFuzzy(int pageFuzzy) {
        PageFuzzy = pageFuzzy;
    }


    public boolean IsDesc() {
        if (StringUtils.isEmpty(SortType) && StringUtils.isEmpty(Sort))
            return true;

        if (Sort.toUpperCase().indexOf("ASC") > 0)
            return false;


        if (SortType.toUpperCase().indexOf("ASC") > 0)
            return false;

        return true;

    }
}
